//
//  YSGMyopiaButtonView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/5.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

enum YSGMyopiaButtonType {
    case reserve
    case scan
    case record
    
    var title: String? {
        switch self {
        case .reserve: return "预约检查"
        case .scan: return "扫码即检"
        case .record: return "检查记录"
        }
    }
    
    var image: UIImage? {
        switch self {
        case .reserve: return UIImage(named: "icon_myopia_reserve")
        case .scan: return UIImage(named: "icon_myopia_scan")
        case .record: return UIImage(named: "icon_myopia_record")
        }
    }
}

typealias TappedHandle = (YSGMyopiaButtonType) -> Void

class YSGMyopiaButtonView: UIView {
    
    var tappedHandle: TappedHandle?
    
    private var buttonType: YSGMyopiaButtonType!

    @IBOutlet private weak var imageView: UIImageView!
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    func updateUI(with buttonType: YSGMyopiaButtonType, closure: TappedHandle?) {
        self.tappedHandle = closure
        self.buttonType = buttonType
        imageView.image = buttonType.image
        titleLabel.text = buttonType.title
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
    }
    
    private func makeUI() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tappedAction))
        addGestureRecognizer(tap)
    }
    
    @objc private func tappedAction() {
        tappedHandle?(buttonType)
    }
}
